package ui;

import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.Timer;

import listener.MainListener;
import config.MainConfig;

public class MainUI extends JFrame {
	public JButton startSignButton;
	public JButton endSignButton;
	public JButton startAnswer;
	public JButton endAnswer;
	public JButton showSign;
	public JButton showAnswer;
	public JButton clearSignedButton;
	public JButton clearAnswerButton;
	public JButton paperButton;
	static String birdString = "Bird";
	static String catString = "Cat";
	static String dogString = "Dog";
	static String rabbitString = "Rabbit";
	static String pigString = "Pig";
	// private JTextField welcomeText;
	// 创建一个容器
	Container ct;
	// 创建背景面板。
	BackgroundPanel bgp;
	private JTextField timeText;
	Graphics graphics;
	public static Image image;

	public MainUI() {
		setResizable(false);
		// 不采用任何布局方式。
		ct = this.getContentPane();
		this.setLayout(null);

		image = new ImageIcon(MainConfig.birdBackPath).getImage();
		bgp = new BackgroundPanel();
		bgp.setBounds(0, 0, MainConfig.MWidth, MainConfig.MHight);
		ct.add(bgp, -1);

		Font fontButton = new Font("SansSerif", Font.BOLD, 15);
		startSignButton = new JButton("开始签到");
		endSignButton = new JButton("结束签到");
		showSign = new JButton("签到情况");
		startAnswer = new JButton("开始答题");
		endAnswer = new JButton("结束答题");
		showAnswer = new JButton("答题情况");
		clearSignedButton = new JButton("清空签到表");
		clearAnswerButton = new JButton("清空答题表");
		paperButton = new JButton("上传试卷");
		
		JRadioButton birdButton = new JRadioButton();
		birdButton.setBorder(null);
		birdButton.setOpaque(false);
		birdButton.setMnemonic(KeyEvent.VK_B);
		birdButton.setActionCommand(birdString);
		birdButton.setSelected(true);

		JRadioButton catButton = new JRadioButton();
		catButton.setOpaque(false);
		catButton.setMnemonic(KeyEvent.VK_C);
		catButton.setActionCommand(catString);

		JRadioButton dogButton = new JRadioButton();
		dogButton.setOpaque(false);
		dogButton.setMnemonic(KeyEvent.VK_D);
		dogButton.setActionCommand(dogString);

		JRadioButton rabbitButton = new JRadioButton();
		rabbitButton.setOpaque(false);
		rabbitButton.setMnemonic(KeyEvent.VK_R);
		rabbitButton.setActionCommand(rabbitString);

		JRadioButton pigButton = new JRadioButton();
		pigButton.setOpaque(false);
		pigButton.setMnemonic(KeyEvent.VK_P);
		pigButton.setActionCommand(pigString);

		// Group the radio buttons.
		ButtonGroup group = new ButtonGroup();
		group.add(birdButton);
		group.add(catButton);
		group.add(dogButton);
		group.add(rabbitButton);
		group.add(pigButton);
		
//		timeText = new JTextField("hello");
//		timeText.setBorder(null);
//		timeText.setBounds(800, 80, 100, 40);
		
		
		GridLayout gridLayout = new GridLayout(4, 4);
		gridLayout.setVgap(10);
		gridLayout.setHgap(10);

		this.setLayout(null);
		JPanel jPanel1 = new JPanel();
		jPanel1.setFocusable(true);
		jPanel1.setBounds(100, 150, 800, 600);
		jPanel1.setLayout(gridLayout);
		jPanel1.setOpaque(false);
		
		JPanel jPanel2 = new JPanel();
		jPanel2.setFocusable(true);
		jPanel2.setBounds(0, 620, 1000, 30);
		jPanel2.setLayout(new FlowLayout());
//		jPanel2.setBackground(Color.RED);
		jPanel2.setOpaque(false);
		
		jPanel2.add(birdButton);
		jPanel2.add(catButton);
		jPanel2.add(dogButton);
		jPanel2.add(rabbitButton);
		jPanel2.add(pigButton);

		startSignButton.setFont(fontButton);
		setIcon(MainConfig.startSignButtonPath, startSignButton);
		
		endSignButton.setFont(fontButton);
		setIcon(MainConfig.endSignButtonPath, endSignButton);
		
		clearSignedButton.setFont(fontButton);
		setIcon(MainConfig.clearSignedButtonPath, clearSignedButton);

		startAnswer.setFont(fontButton);
		setIcon(MainConfig.startAnswerPath, startAnswer);
		
		endAnswer.setFont(fontButton);
		setIcon(MainConfig.endAnswerPath, endAnswer);
		
		clearAnswerButton.setFont(fontButton);
		setIcon(MainConfig.clearAnswerButtonPath, clearAnswerButton);

		showSign.setFont(fontButton);
		setIcon(MainConfig.showSignPath, showSign);
		
		showAnswer.setFont(fontButton);
		setIcon(MainConfig.showAnswerPath, showAnswer);
		
		paperButton.setFont(fontButton);
		setIcon(MainConfig.paperButtonPath, paperButton);
		
		jPanel1.add(startSignButton);
		jPanel1.add(endSignButton);
		jPanel1.add(showSign);

		jPanel1.add(startAnswer);
		jPanel1.add(endAnswer);
		jPanel1.add(showAnswer);

		jPanel1.add(clearSignedButton);
		jPanel1.add(clearAnswerButton);
		jPanel1.add(paperButton);

		MainListener mainListener = new MainListener(this);
		startSignButton.addActionListener(mainListener);
		endSignButton.addActionListener(mainListener);
		startAnswer.addActionListener(mainListener);
		endAnswer.addActionListener(mainListener);
		showSign.addActionListener(mainListener);
		showAnswer.addActionListener(mainListener);
		clearSignedButton.addActionListener(mainListener);
		clearAnswerButton.addActionListener(mainListener);
		paperButton.addActionListener(mainListener);
		birdButton.addActionListener(mainListener);
		catButton.addActionListener(mainListener);
		dogButton.addActionListener(mainListener);
		rabbitButton.addActionListener(mainListener);
		pigButton.addActionListener(mainListener);

		this.add(jPanel1, 0);
		this.add(jPanel2, 0);
		this.setTitle("中天模拟键盘");
//		this.add(timeText);
		// 设置窗体的大小
		this.setSize(MainConfig.MWidth, MainConfig.MHight);

		// 设置关闭窗体时，进程结束
		this.setDefaultCloseOperation(3);
		// 设置窗体在屏幕的位置
		this.setLocationRelativeTo(null);
//		this.setVisible(true);
		
		ActionListener animate = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				repaint();
			}
        };
        Timer timer = new Timer(500,animate);
        timer.start();
		
	}

	public static void main(String[] args) {
		new MainUI().setVisible(true);
	}

	/**
	 * 绘制背景图片
	 * 
	 * @author wj
	 * 
	 */
	class BackgroundPanel extends JPanel {
		public void paintComponent(Graphics g) {
			super.paintComponent(g);
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);
			Font fontText = new Font("SansSerif", Font.BOLD, 28);
			g.setFont(fontText);
			g.setColor(Color.WHITE);
			drawString(g, "欢迎使用中天模拟键盘", 100, 80);
			drawString(g, getNowTime(), 800, 80);
		}
	}
	
	/*
	 * 获取当前时间
	 */
	private String getNowTime() {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); 
		return formatter.format(new Date().getTime());
	}

	/**
	 * 绘制文字
	 * @param g
	 * @param str
	 * @param xPos
	 * @param yPos
	 */
	public void drawString(Graphics g, String str, int xPos, int yPos) {
		g.drawString(str, xPos, yPos);
	}
	
	/**
	 * 绘制图片
	 * @param file
	 * @param com
	 */
	public void setIcon(String file,JButton com){ 
        ImageIcon ico=new ImageIcon(file); 
        Image temp=ico.getImage().getScaledInstance(280,140,ico.getImage().SCALE_DEFAULT);
        ico=new ImageIcon(temp); 
        com.setIcon(ico); 
    } 
}
